var o;
avalon.ready(function () {
    var m = "";
    o = avalon.define({
        $id: "root",
        register: false,
        codenumber: false,
//      notice:false,
        wangji: false,
        shouquan: false,
        codeMsg: false,
        setpw: false,
        login: false,
        isaccede: false,
        useerror: false,
        kind: 0,  //0为注册，默认为注册
        /*立即注册弹框*/
        OpenRegister: function () {
            o.register = true;
        },
        /*打开验证码弹框*/
        OpenCode: function () {

            $("#span").html("");
            // 校验手机号
            // 注册点击下一步
            var mobile = $("#mobile").val();
            var msg = "请输入正确手机号";
            var mobileReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
            // /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/
            if (mobile == null || mobile == ""
                || !mobileReg.test(mobile.trim())) {
                $("#span").html("<font color='red'>" + msg + "</font>");
                return;
            }
            $.ajax({
                type: "POST",//请求方式
                dataType: "json",//返回数据格式
                url: "/checkMobile",//接口地址
                data: {//传入参数
                    "mobile": mobile
                },
                error: function (request) {//请求错误结果
                    alert("Your request is wrong!!!");
                },
                success: function (data) {//请求正确结果
                    if (200 == data.code) {
                        m = mobile;
                        o.register = false;
                        o.codenumber = true;
                        o.kind = 0;
                        $("#span").html("<font color='red'>" + data.msg + "</font>");
                    } else {
                        $("#span").html("<font color='red'>" + data.msg + "</font>");
                    }
                }
            });

        },
        Getcode: function (obj) {
            settime(obj);
            // 发送验证码
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "/sendMobileMsg",
                data: {
                    "mobile": m
                },
                error: function (request) {
                    alert("Your request is wrong!!!");
                },
                success: function (data) {
                    if (200 == data.code) {
                    }
                }
            });
            o.codenumber = true;
        },
        /*登录弹框*/
        OpenLogin: function () {
            o.login = true;
            o.kind = 1;   //数字为1代表是登录点过去的
        },
        /*忘记密码弹框*/
        forgetpass: function () {
            o.login = false;
            o.wangji = true;
            o.codenumber = true;
        },
        /*验证码弹框过渡到设置密码弹框*/
        OpenNotice: function () {
            if (o.kind == 0 && o.isaccede) {
                // 校验验证码
                var msgCode = $("#msgcode").val();
                if (isEmpty(msgCode)) {
                    return;
                }
                $.ajax({
                    url: "/checkMobileMsg",
                    type: "post",
                    dataType: "json",
                    data: {
                        "msgCode": msgCode,
                        "mobile": m
                    },
                    success: function (data) {
                        if (data.code == 200) {
                            o.codenumber = false;
                            o.setpw = true;
                        } else {
                            o.codeMsg = true;
                            return false;
                        }

                    }
                })
            } else if (o.kind == 0 && !o.isaccede) {
                //未勾选授权
                o.shouquan = true
                return false;
            } else if (o.kind == 1 && o.wangji) {
                o.codenumber = false;
                o.setpw = true;
            } else if (o.kind == 1 && !o.wangji) {
                //登录
                // 校验手机号密码是不是空
                var mobile = $("#loginMobile").val();
                var password = $("#loginPassword").val();
                if (isEmpty(mobile)) {
                    return;
                }
                if (isEmpty(password)) {
                    return;
                }
                $.ajax({
                    url: "loginIn",
                    data: {
                        "mobile": mobile,
                        "password": password,
                        "change": "1"
                    },
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 200) {
                            window.location.href = "/trade/order_list";
                        } else if (data.code == 4002) {
                            window.location.href = "/user/index";
                        } else {
                            o.useerror = true;
                            return false;
                        }
                    }

                });
                /*o.codenumber = false;
                location.href="../products/order_list.html";  //跳转至指定的页面
*/
            }
        },
        /*是否勾选注册协议*/
        ChooseAccede: function () {
            o.isaccede = !o.isaccede;
        },

        toChicang: function () {
            //立即注册
            // 密码立即注册
            var psword = $("#psword").val();
            $("#myspan2").html("");
            $.ajax({
                url: "/register",
                type: "post",
                data: {
                    "mobile": m,
                    "password": psword
                },
                dataType: "json",
                success: function (data) {
                    if (data.code == 200) {
                        window.location.href = "/trade/index";
                    }else{
                        $("#myspan2").html("<font color='red'>" + data.msg + "</font>");
                    }

                }
            })
            /*o.setpw = false;
            location.href="../products/order_list.html";  //跳转至指定的页面
*/
        },
//      OpenSetpw:function(){
//      	o.notice = false;
//      	o.setpw = true;
//      },
        /*关闭弹框*/
        closepop: function () {
            o.register = false;
            o.codenumber = false;
//      	o.notice = false;
            o.setpw = false;
            o.login = false;
        }
    });
    Menu.ready(function () {
        Menu.current(4);
    });
    avalon.scan(document.body);
    $("#account").click(function () {
        window.location.href = '/user/accountCenter';
    });
    $("#capital").click(function () {
        window.location.href = '/user/toCapital';
    });
    $("#blitz").click(function () {
        window.location.href = '/user/toFast_orderSet';
    });
    $("#common").click(function () {
        window.location.href = '/user/tochangjian';
    });
    $("#exit").click(function () {
        window.location.href = '/loginOut';
    });
    $("#putforward").click(function () {
        window.location.href = '/user/toTixian';
    });
    avalon.scan(document.body);
})
//定义一个60秒计时器变量
var countdown=60;
//构造一个倒计时函数叫settime
function settime(obj) {
    //开始判断倒计时是否为0
    if (countdown == 0) {
        obj.removeAttribute("disabled");
        obj.value="发送验证码";
        countdown = 60;
        //立即跳出settime函数，不再执行函数后边的步骤
        return;
    } else {
        obj.setAttribute("disabled", true);
        obj.value="重新发送(" + countdown + ")";
        countdown--;
    }
    //过1秒后执行倒计时函数
    setTimeout(function() {
        settime(obj)
    },1000)
}

function limitInput(o) {
    var value = o.value;
    var msg = "密码是6-12位数字、字母组合";
    var reg = /^[0-9a-zA-Z]+$/;
    if (!reg.test(value)) {
        $("#myspan2").html("<font color='red'>" + msg + "</font>");
        return;
    }
    var min = 6;
    var max = 12;
    if (value.length < min || value.length > max) {
        $("#myspan2").html("<font color='red'>" + msg + "</font>");
        return;
    }
}

function isEmpty(obj) {
    if (typeof obj == "undefined" || obj == null || obj.trim() == "") {
        return true;
    } else {
        return false;
    }
}

